package edu.ccut.saturn.studio.saturndata.page;

import java.util.List;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;


import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.database.model.DBConnectionProvider;
import edu.ccut.saturn.studio.saturndata.editor.SaturnDataEditor;
import edu.ccut.saturn.studio.saturndata.model.SaturnData;
import edu.ccut.saturn.studio.saturndata.model.SaturnDataModel;

/**
 * 字段页
 * 
 * @author 谷钰
 * 
 * @version 1.0 2008.12.09
 * 
 */
public class DataItemPage extends FormPage {
	private DateItemMasterDetailsBlock block;
	private Section dbConSection;

	private SaturnData saturnData;
	private SaturnDataModel saturnDataModel;

	public DataItemPage(SaturnDataEditor editor) {
		super(editor, "field", Messages.getString("FieldPage.field")); //$NON-NLS-1$ //$NON-NLS-2$

		this.saturnDataModel = editor.getSaturnDataModel();
		this.block = new DateItemMasterDetailsBlock(this, editor);
		this.saturnData = saturnDataModel.getSaturnData();
	}

	public SaturnDataModel getSaturnDataModel() {
		return saturnDataModel;
	}

	protected void createFormContent(IManagedForm managedForm) {
		ScrolledForm form = managedForm.getForm();
		FormToolkit toolkit = managedForm.getToolkit();
		form.setText(Messages.getString("FieldPage.datafield")); //$NON-NLS-1$

		GridLayout gridlayout = new GridLayout(1, true);
		form.getBody().setLayout(gridlayout);
		GridData griddata = new GridData(1808);
		griddata.heightHint = 100;
		form.getBody().setLayoutData(griddata);

		this.block.createContent(managedForm);
		createDatasourceSection(form, toolkit);
	}
	
	@Override
	public boolean isDirty() {
		return false;
	}

	private void createDatasourceSection(ScrolledForm scrolledform,
			final FormToolkit toolkit) {

		Composite composite = toolkit.createComposite(scrolledform.getBody());
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(768));
		dbConSection = toolkit.createSection(composite, 386);
		dbConSection.setActiveToggleColor(toolkit.getHyperlinkGroup()
				.getActiveForeground());
		dbConSection.setToggleColor(toolkit.getColors().getColor(
				"org.eclipse.ui.forms.SEPARATOR")); //$NON-NLS-1$
		dbConSection.setText(Messages.getString("FieldPage.choosedatabaselink")); //$NON-NLS-1$
		dbConSection
				.setDescription(Messages.getString("FieldPage.currentdatabaselinkis") + saturnData.getConnectionName()); //$NON-NLS-1$
		dbConSection.setLayout(new GridLayout(1, false));
		dbConSection.setLayoutData(new GridData(1816));
		Composite composite1 = toolkit.createComposite(dbConSection, 0);
		dbConSection.setClient(composite1);
		composite1.setLayout(new GridLayout(3, false));
		composite1.setLayoutData(new GridData(768));
		toolkit.paintBordersFor(composite1);

		Table table = toolkit.createTable(composite1, 66308);
		table.setLayout(new GridLayout());
		GridData griddata = new GridData(1792);
		griddata.heightHint = 100;
		table.setLayoutData(griddata);
		toolkit.paintBordersFor(table);
		TableViewer dbConTableViewer = new TableViewer(table);
		dbConTableViewer.setContentProvider(new SaturnSWTUtils
				.SaturnTableViewerDefaultContentProvider());
		dbConTableViewer.setSorter(new ViewerSorter());
		dbConTableViewer.setLabelProvider(new SaturnSWTUtils.SaturnTableViewerDefaultLabelProvider() {

			@Override
			public String getColumnText(Object obj, int index) {
				if (obj != null) {
					return obj.toString();
				}
				
				return super.getColumnText(obj, index);
			}
			
		});

		List<String> connectionNames = DBConnectionProvider.getInstance()
				.getAllDBConnectionNames();

		dbConTableViewer.setInput(connectionNames);

		if (connectionNames != null) {
			for (String name : connectionNames) {
				if (name.equals(saturnData.getConnectionName())) {
					dbConTableViewer
							.setSelection(new StructuredSelection(name));
				}
			}
		}

		dbConTableViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {

					public void selectionChanged(
							SelectionChangedEvent selectionchangedevent) {
						IStructuredSelection istructuredselection = (IStructuredSelection) selectionchangedevent
								.getSelection();

						if (istructuredselection.getFirstElement() instanceof String) {
							String selectedName = (String) istructuredselection
									.getFirstElement();

							dbConSection.setDescription(Messages
									.getString("FieldPage.currentdatabaselinkis") //$NON-NLS-1$
									+ selectedName);

							saturnData.setConnectionName(selectedName);
							block.setConnectname(selectedName);
							saturnDataModel.changeDataAttributeValue(
									SaturnDataModel.DATA_ATTR_CONNECTNAME,
									saturnData.getConnectionName());

							SaturnProjectUtil.refreshProject(DataItemPage.this
									.getEditorInput());
						}
					}
				});
	}

}
